package com.example.demo.service;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import com.example.demo.pojo.ResponseResult;
import com.example.demo.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@Service
public class ThreadService {

    @Async("doSomethingExecutor")
    public CompletableFuture<Map> excuteNumbercheck(String phoneNumber) {
        Map data = new HashMap();
        log.info(Thread.currentThread().getName() + "线程开始");
        try {
            Map params = new HashMap();
            params.put("state", "A3540588%2Fl6x9");
            params.put("phone", phoneNumber);
            HttpResponse response = HttpRequest.post("http://weixin.170cz.cn/weixin/qm/numbercheck")
                    .form(params).execute();
            data = JSONUtil.readValue(response.body(), Map.class);
//            log.info(Thread.currentThread().getName() + "号码归属数据{}", data);
        } catch (RestClientException e) {
            e.printStackTrace();
            CompletableFuture.completedFuture(new ResponseResult(HttpStatus.HTTP_INTERNAL_ERROR, "查询接口异常", data));
        }
        return CompletableFuture.completedFuture(data);
    }

    @Async("doSomethingExecutor")
    public CompletableFuture<Map> excuteBalance(String phoneNumber) {
        Map data = new HashMap();
        log.info(Thread.currentThread().getName() + "线程开始");
        try {
            Map params = new HashMap();
            params.put("state", "A3540588%2Fl6x9");
            params.put("phone", phoneNumber);
            HttpResponse response = HttpRequest.post("http://weixin.170cz.cn/weixin/query/balance")
                    .form(params).execute();
            data = JSONUtil.readValue(response.body(), Map.class);
//            log.info(Thread.currentThread().getName() + "号码话费数据{}", data);
        } catch (RestClientException e) {
            e.printStackTrace();
            CompletableFuture.completedFuture(new ResponseResult(HttpStatus.HTTP_INTERNAL_ERROR, "查询接口异常", data));
        }
        return CompletableFuture.completedFuture(data);
    }

}
